//main object 
var powaTolkien = {

	/*================
	All settings
	================*/
	settings : {
		
		Global:{
			
			menuitems:{
				
				menuWapper:'#headerContainer .wrapperMenu',
				
			}
			
		},
		
		pageClass:{
			
			homePage:'pageHome',
			categoryPage: 'pageCategory',
			productDetails: 'pageProductDetails',
			searchResults:'pageProductSearchResults',
			quickCart:'.pageBillingDetails, .pageShippingDetails, .pagePaymentDetails, .pageCreditCardDetails',
			billingShiping:'.pagePaymentDetails, .pageOrderConfirmation, .pageOrderDetails', //order summary, order confirmation and account order details pages
			
			featuresNews: 'Features---News',
			
			locayta: 'Locayta-static'
			
			
		},
		
		// all caarousel settings
		carousel:{
			
			common:{
				
				itemsWrapperClass:'featureSet'
				
			},
			
			// the hero carousel on the home page
			hero:{
				//Settings for hero slider settings
				element: '#hero #images .cont',
				
				fx:     'scrollHorz', 
				speed:  1000, 
				timeout: 4000, 
				next:   '#hero #Next', 
				prev:   '#hero #Prev',
				pager:  '#hero #pager',
				pause: 1,
				sync:  1,
				rev:0,
			},
			
			//settings for the featured carousel on the home page
			featured:{
				// featured slider settings
				element: '.featuredProductListThbSection',
				
				fx:     'scrollHorz', 
				speed:  1000, 
				timeout: 0, 
				next:   '.featuredProductListThbSection #Next', 
				prev:   '.featuredProductListThbSection #Prev',
				pager:  '.featuredProductListThbSection #pager',
				pause: 1,
				sync:  1,
				rev:0,
			},
			
			related:{
				// featured slider settings
				element: '.relatedProductListThbSection .section',
				
				fx:     'scrollHorz', 
				speed:  1000, 
				timeout: 0, 
				next:   '#linked_products #Next', 
				prev:   '#linked_products #Prev',
				pager:  '#linked_products #pager',
				pause: 1,
				sync:  1,
				rev:0,
			}
		},
		
		Home:{
			//bottom promo blocks
			promoBlockBtm:'div.promoBlockBtm', 
			textSection: 'div.textSection',
			},
			
		FeatureNews:{
			//all promo blocks
			promoBlock:'div.promoBlock', 
			textSection: 'div.textSection',
			},
			
		ProductDetails:{
			// Product detail tabs
			productDescriptionText: 'div.productDetailsSection div.productDescriptionText',
			tabsWrapper: 'div.tabsWrapper',
			tabsNav: 'ul.tabs-nav',
			tabPane: 'div.tabs-panes',
			reviews: 'div.reviews',
			reviewsLI: 'li.reviews',
			buy: 'div.buyBlock',
			relatedProducts:'#linked_products'
			
			},
		Locayta:{
			
			productItem: '.productListThbSection .productListThbBlock'
			
			
			}
		
		
	},
	/*=======================
	Common functions 
	=======================*/	
	common:{
		
		currentPage: function(){
			var page = $('body').attr('class');
			return page;
		},
		
		mainMenu: function(){
			
			// text replace Win link
			$(".wrapperMenu #staticItem_1 a span.name").html('Win');
			
			//calculate correct width for last menu item
			var homePage = $(".wrapperMenu #homePage").width();
			var cItem_3 =   $(".wrapperMenu #categoryItem_3").width();
			var cItem_4 =	$(".wrapperMenu #categoryItem_4").width();
			var cItem_5 =	$(".wrapperMenu #categoryItem_5").width();
			var cItem_6 =	$(".wrapperMenu #categoryItem_6").width();
			
			var cItem_7 =	$(".wrapperMenu #categoryItem_7").width();
			var cItem_8 =	$(".wrapperMenu #categoryItem_8").width();
			var cItem_9 =	$(".wrapperMenu #categoryItem_9").width();
			var sItem_1 =	$(".wrapperMenu #staticItem_1").width();
			var sItem_2 =	$(".wrapperMenu #staticItem_2").width();
			
			var lastItem_1 =  980 - (homePage + cItem_3 + cItem_4 + cItem_5);
			var lastItem_2 =  980 - (cItem_7 + cItem_8 + cItem_9 + sItem_1);
			
			//$(".wrapperMenu #categoryItem_6").css('width',''+lastItem_1+'px');
			//$(".wrapperMenu #staticItem_2").css('width',''+lastItem_2+'px');
				
			
		},
		
		textMenu: function(){
			
			$(".textMenu #6 a").attr('href','#');
			$(".textMenu #6 a").attr('rel','#signup');
			$(".textMenu #6 a[rel]").overlay();
		},
		
		signUp: function(){
			
			$("#headerContainer #topBanners .bannerLeft a[rel]").overlay();
			
			$("#middleContainer .orderDetailsSection .cont a[rel]").overlay();
		},
		
		siteMap: function(){
			$('div.webSiteMapSection li a:contains("Main Series POD")').parent().remove();
			$('div.webSiteMapSection li a:contains("Audio Books")').parent().remove();
			$('div.webSiteMapSection li a:contains("PCI Compliant")').parent().remove();
			$('div.webSiteMapSection li a:contains("Password Strength Policy")').parent().remove();
		},
		
		billingShippingDetailsManipulation: function(){
			$('#middleContainer div.wrapperBillingDet').clone().appendTo('div.wrapperBillingShippingDets'); //clone billing details. If shipping exists, remove clone
			$('div.wrapperBillingDet:last').addClass('wrapperBillingDetExtra').css('display','block'); //fail-safe (can be buggy)
			$('div.wrapperBillingDet:last').each(function(index, element) {
					$(this).find('thead th.first').html('Shipping Details'); //rename heading
					$(this).find('thead th:last a').remove(); //remove last link
			});		
			if ($('#middleContainer .centerContainer div.wrapperShippingDet').length > 0){
				$('div.wrapperBillingDet:last').remove();
			}
		},
		
		quickCartClone: function(){		
			var quickcart = $('#middleContainer td.rightContainer div.quickCartSection');
			var headerContainer = $('#headerContainer div.content');
			var headerContainerAccountMenu = $(headerContainer).find('div.accountMenu');
			$(quickcart).clone().insertBefore(headerContainerAccountMenu);
		}
		
	},
	/*=======================
	All Featurs and function 
	=======================*/
	feature: {
		
		//All Carousels
		carousel: {
			
			// The hero Carousel
			hero: function(){
				
				// remove unwanted markup
				var aTags = $(powaTolkien.settings.carousel.hero.element + ' a');
				if ( aTags.parent().is(powaTolkien.settings.carousel.hero.element + ' p') ) {
				aTags.unwrap();
				}
					
				// run the hero slider
				$(powaTolkien.settings.carousel.hero.element).cycle({ 
				fx:     powaTolkien.settings.carousel.hero.fx, 
				speed:  powaTolkien.settings.carousel.hero.speed, 
				timeout: powaTolkien.settings.carousel.hero.timeout, 
				next:   powaTolkien.settings.carousel.hero.next, 
				prev:   powaTolkien.settings.carousel.hero.prev,
				pager:  powaTolkien.settings.carousel.hero.pager,
				pause:  powaTolkien.settings.carousel.hero.pause,
				sync:   powaTolkien.settings.carousel.hero.sync,
				rev: 	powaTolkien.settings.carousel.hero.rev
				});
			},
			
			//The featured carousel
			featured: function(){
				
				$(powaTolkien.settings.carousel.featured.element +' .nofloat').remove();
				$(powaTolkien.settings.carousel.featured.element +' script').remove();
				
				var featured = $(powaTolkien.settings.carousel.featured.element +' .block');
				for(var i = 0; i < featured.length; i+=4) {	
	  	  		featured.slice(i, i+4).wrapAll('<div class=" ' + powaTolkien.settings.carousel.common.itemsWrapperClass + ' "></div>');	
				}
				
				// run the featured slider
				$(powaTolkien.settings.carousel.featured.element + '> .cont:eq(0)').cycle({ 
				fx:     powaTolkien.settings.carousel.featured.fx, 
				speed:  powaTolkien.settings.carousel.featured.speed, 
				timeout: powaTolkien.settings.carousel.featured.timeout, 
				next:   powaTolkien.settings.carousel.featured.next, 
				prev:   powaTolkien.settings.carousel.featured.prev,
				pager:  powaTolkien.settings.carousel.featured.pager,
				pause:  powaTolkien.settings.carousel.featured.pause,
				sync:   powaTolkien.settings.carousel.featured.sync,
				rev: 	powaTolkien.settings.carousel.featured.rev
				});
			},
			
			//related products carousel START
			related: function(){	
			
				$(powaTolkien.settings.carousel.related.element +' .nofloat').remove();
				$(powaTolkien.settings.carousel.related.element +' script').remove();
				$(powaTolkien.settings.carousel.related.element +' #nav').remove();
				
				var featured = $(powaTolkien.settings.carousel.related.element +' .relatedProductListThbBlock');
				for(var i = 0; i < featured.length; i+=4) {	
	  	  		featured.slice(i, i+4).wrapAll('<div class=" ' + powaTolkien.settings.carousel.common.itemsWrapperClass + ' "></div>');	
				}
				
				// run the featured slider
				$(powaTolkien.settings.carousel.related.element).cycle({ 
				fx:     powaTolkien.settings.carousel.related.fx, 
				speed:  powaTolkien.settings.carousel.related.speed, 
				timeout: powaTolkien.settings.carousel.related.timeout, 
				next:   powaTolkien.settings.carousel.related.next, 
				prev:   powaTolkien.settings.carousel.related.prev,
				pager:  powaTolkien.settings.carousel.related.pager,
				pause:  powaTolkien.settings.carousel.related.pause,
				sync:   powaTolkien.settings.carousel.related.sync,
				rev: 	powaTolkien.settings.carousel.related.rev
				});
					
			}
		}
	},
	
	/*================
	All Page specific
	================*/
	page: {
		
			Home:{
				
				promoBlockBtmInit: function() { //bottom promo blocks
				
						if($(powaTolkien.settings.Home.promoBlockBtm).length > 0){
						$(powaTolkien.settings.Home.promoBlockBtm+' '+powaTolkien.settings.Home.textSection+':last').addClass('textSectionLast');
						$(powaTolkien.settings.Home.promoBlockBtm+' '+powaTolkien.settings.Home.textSection).each(function(index, element) {
				  		$(this).find('a:last').addClass('last');	
						});
						}
				}
	
			},
			
			ProductDetails: {
				
				productInfoTabs: function() {
				
						if ($(powaTolkien.settings.ProductDetails.productDescriptionText).is(':empty')){ //if description is empty, restyle
							$(powaTolkien.settings.ProductDetails.tabsWrapper).css('display','none');
						}
					
						//product description tabs
						var tabContent1 = $(powaTolkien.settings.ProductDetails.productDescriptionText+' div:eq(0)').html();
						var tabContent2 = $(powaTolkien.settings.ProductDetails.productDescriptionText+' div:eq(1)').html();
						var tabContent3 = $(powaTolkien.settings.ProductDetails.productDescriptionText+' div:eq(2)').html();
									
						if ($(powaTolkien.settings.ProductDetails.productDescriptionText+' div:eq(0)').length > 0){ // if div (tab) exists in the product description
						 
							$(powaTolkien.settings.ProductDetails.tabsWrapper).css('display','block');
							
							if ($(powaTolkien.settings.ProductDetails.productDescriptionText+' div:eq(1)').length > 0){ //if the 2nd tab content exists, prepend to the tabs
								$(powaTolkien.settings.ProductDetails.tabsWrapper).css('display','block');
								$(powaTolkien.settings.ProductDetails.reviews).empty().prepend(tabContent2);
							}
							else {
								$(powaTolkien.settings.ProductDetails.tabsNav+' '+powaTolkien.settings.ProductDetails.reviewsLI).remove();
							}
						
							$(powaTolkien.settings.ProductDetails.productDescriptionText).find('div:eq(1), div:eq(2)').remove();
							$(function() { //tabs - active states
									$(powaTolkien.settings.ProductDetails.tabsNav+ ' li a').tabs(powaTolkien.settings.ProductDetails.tabPane+' > div'); // setup ul.tabs to work as tabs for each div directly under div.panes
							});	
						}
						else { //if div (tab) does not exist in the product description
							$(powaTolkien.settings.ProductDetails.tabsNav+' '+powaTolkien.settings.ProductDetails.reviewsLI).remove();
						}
					
						if ($(powaTolkien.settings.ProductDetails.productDescriptionText).is(':empty')){ //if description is empty, restyle
							$(powaTolkien.settings.ProductDetails.tabsWrapper).css('display','none');
							$(powaTolkien.settings.ProductDetails.buy).css({'margin':'15px 0 0 0'});
								
										
						}
					
					},
					
					wasPrice: function(){
						
						$('div.productAttribute label:contains("Was Price")').parent().addClass('wasPrice').hide();
						var getwasPrice = $('div.wasPrice span').text();
						//getwasPrice = getwasPrice.slice(0, -2);
						//getwasPrice = getwasPrice+'.00';
						
						if(getwasPrice != ''){
						$('.productPriceBlock .total').append('<span class="priceWas">RRP £'+getwasPrice+'</span>');
						}
							
							
						
					},
					
					productRelated: function() {
						
						if ($(powaTolkien.settings.ProductDetails.relatedProducts +'#section_1 .section'  && powaTolkien.settings.ProductDetails.relatedProducts +'#section_2 .section').is(':empty')){ //if related products is empty, hide all
							$(powaTolkien.settings.ProductDetails.relatedProducts).css('display','none');
						}
						
					},
					
					
					popupsInit: function(){ //popups init
								$('#imgPopUpWrapper a.close').append('<span>X</span>'); //append close text
								$('div.hidden').click(function(){ //overlay - close popups
									$('#largeImagePopup[rel]').overlay().close();
								});
					},
					
					preorder: function(){
						
						$('div.productAttribute label:contains("IsPreorder")').parent().addClass('preOrder').hide();
						var getPreOrder = $('div.preOrder span').text();
						
						if (getPreOrder == 'true'){
							
							$('#buy_button').attr('value','Pre Order');							
						}
						
						
					},
					
					printOnDemand: function(){
						
						$('div.productAttribute label:contains("Print on Demand")').parent().addClass('printOnDemand');
						
						var getPrintOnDemand = $('div.printOnDemand label').text();
						
						if (getPrintOnDemand == 'Print on Demand: '){
							
							$('.productInfoTop .qty').hide();							
						}
						$('.productInfoTop .qty').hide();
					}
				},
			
			ProductSearchResults:{
				
				noProducts: function(){
					
					if($('#no_products_found').length == !0){
					$('body').addClass('no-search-results');
					$('#middleContainer .leftContainer').css('disply','none');
					
					
					}	
					
				}
				
			},
			
			FeatureNews:{
				
				features: function(){
					
					if($(powaTolkien.settings.FeatureNews.promoBlock).length > 0){
						$(powaTolkien.settings.FeatureNews.promoBlock).each(function( ){
							$(powaTolkien.settings.FeatureNews.promoBlock+' '+powaTolkien.settings.FeatureNews.textSection).each(function() {
							$(this).find('a:last').addClass('last');	
							
							
							});
						
						});
					}

				}
				
			}
			
		}
}// end main object



//doc ready
$(document).ready(function() {
	
	 //Global
	 powaTolkien.common.signUp();
	 powaTolkien.common.textMenu();
	 powaTolkien.common.mainMenu();
	 powaTolkien.common.siteMap();
	
	
	 //home page
	if($('body').hasClass(powaTolkien.settings.pageClass.homePage)){
	
		powaTolkien.feature.carousel.hero();
		powaTolkien.feature.carousel.featured();
		powaTolkien.page.Home.promoBlockBtmInit();
		
		// run after locayta product have loaded
		LM.complete(function(){
			powaTolkien.feature.carousel.featured();
		});
		
		
	}
	
	 //category page
	if($('body').hasClass(powaTolkien.settings.pageClass.categoryPage)){
		if($('#footerContainer .textSection').length > 0){ //if more than 1 textSection (category banner), add class
			$('#footerContainer .textSection:first').addClass('categoryBannerBtm');
		}
	}
	
	 //product details page
	if($('body').hasClass(powaTolkien.settings.pageClass.productDetails)){
	
	LM.complete(function(){
		powaTolkien.feature.carousel.related();
		powaTolkien.page.ProductDetails.productRelated();
		
	});	
		powaTolkien.page.ProductDetails.productInfoTabs();
		powaTolkien.page.ProductDetails.popupsInit();
		powaTolkien.page.ProductDetails.preorder();
		powaTolkien.page.ProductDetails.wasPrice();
		powaTolkien.page.ProductDetails.printOnDemand();
	
	}
	
	//Search results page
	if($('body').hasClass(powaTolkien.settings.pageClass.searchResults)){
		LM.complete(function(){
		powaTolkien.page.ProductSearchResults.noProducts();
		});
	}
	
	//Cart pages with Quick cart
	if($('body').is(powaTolkien.settings.pageClass.quickCart)){
		
		powaTolkien.common.quickCartClone();
	}
	
	//Billing shipping details
	if($('body').is(powaTolkien.settings.pageClass.billingShiping)){
		powaTolkien.common.billingShippingDetailsManipulation();
	}
	
	//Features and News
	if($('body').hasClass(powaTolkien.settings.pageClass.featuresNews)){
		
		powaTolkien.page.FeatureNews.features();
		LM.complete(function(){
			powaTolkien.feature.carousel.featured();
		});
		
	}
	
	
	if ($(document.body).hasClass('pageCreditCardDetails')){
			
		$('#realButton, #popupMessage, .hidden-card-details').hide();
		$('#fakeButton').show();
	
		$('#agreeCheckbox').click(function(){
			if(this.checked){
				$('#fakeButton').hide();
				$('#realButton').show();
				}
			else{
				$('#realButton').hide();
				$('#fakeButton').show();
				};
			});
			
			$('#fakeButton').click(function(){
				$('.hidden-card-details').show();
				$('#popupMessage').show("fast");
					return false;
				});
				
			$('#popupMessage').find('.close').click(function(){
				$('#popupMessage, .hidden-card-details').hide();
			});
			
			$('.hidden-card-details').click(function(){
				$('#popupMessage, .hidden-card-details').hide();
			});
		}
	
	
});


	